package com.tianzj.boot.mvc.service.system.impl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.tianzj.boot.mvc.entity.system.SysUser;
import com.tianzj.boot.mvc.repository.system.SysUserRepository;
import com.tianzj.boot.mvc.service.system.SysUserService;
import com.tianzj.boot.util.BeanCopyUtil;

@Service
public class SysUserServiceImpl  implements SysUserService{

	@Autowired
	private SysUserRepository userRepository;

	@Override
	public void addUser(SysUser sysUser) {
		userRepository.save(sysUser);
	}

	@Override
	public SysUser getUserByUserName(SysUser user) {
		return userRepository.findByUserName(user.getUserName());
	}

	@Override
	public Page<SysUser> getSysUserList(final SysUser sysUser,Pageable pageable) {
		 Specification<SysUser> specification = new Specification<SysUser>() {  
	            @Override  
	            public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {  
	                Path<String> userName = root.get("userName");  
	                Path<Long> createDate = root.get("createDate");  
	                Path<Long> status = root.get("status");  
	                Predicate predicate = null;
	                if(StringUtils.isNotBlank(sysUser.getUserName())){
	                	  predicate =cb.and(cb.like(userName, "%"+sysUser.getUserName()+"%"));
	                }
	                if(sysUser.getStartTime()!=null&&sysUser.getEndTime()!=null){
	                	Predicate predicate2=cb.and(cb.between(createDate, sysUser.getStartTime(), sysUser.getEndTime()));
	                	 predicate = getPre(cb, predicate, predicate2);
	                }
	                if(sysUser.getStatus()!=null){
	                	Predicate predicate2=cb.and(cb.equal(status, sysUser.getStatus()));
	                	 predicate = getPre(cb, predicate, predicate2);
	                }
	                Predicate predicate2 = cb.notEqual(status, 9);
	                predicate = getPre(cb, predicate, predicate2);
	                return predicate;  
	            }
			 
	        };  
	    Page<SysUser> sysUsers = userRepository.findAll(specification,pageable);
		return sysUsers;
	}
	
	private Predicate getPre(CriteriaBuilder cb, Predicate predicate, Predicate predicate2) {
		if(predicate!=null){
			 predicate=cb.and(predicate, predicate2);
		 }else{
			 predicate=predicate2;
		 }
		return predicate;
	} 
	
	
	@Override
	public SysUser getSysUserById(Long id) {
		return userRepository.findOne(id);
	}

	
	@Override
	public void updateUser(SysUser sysUser) {
		SysUser  user= userRepository.findOne(sysUser.getId());
		BeanUtils.copyProperties(sysUser, user, BeanCopyUtil.getNullPropertyNames(sysUser));
		userRepository.save(user);
	}

	
	@Override
	@Transactional
	public void deleteSysUserById(String ids) {
		if(StringUtils.isNotBlank(ids)){
			String[] idsArray = ids.split(","); 
			for (String id : idsArray) {
				SysUser  user= userRepository.findOne(Long.parseLong(id));
				user.setStatus(9);
				userRepository.save(user);
			}
		}
	}


	@Override
	public SysUser getByUserNameAndPass(SysUser userNew) {
		return userRepository.findByUserNameAndPassword(userNew.getUserName(),userNew.getPassword());
	}
}
